package com.sl.mes.db.service.mm;

import com.sl.mes.db.dao.MmPickmBMapper;
import com.sl.mes.db.dao.MmPickmMapper;
import com.sl.mes.db.itf.mm.IMmPickm;
import com.sl.mes.db.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MmPickmDaoService implements IMmPickm {

    private final MmPickm.Column[] result = new MmPickm.Column[]
            { MmPickm.Column.cfirstbillrowid ,MmPickm.Column.cpickmid,MmPickm.Column.vbillcode};

    private final MmPickmB.Column[] result1 = new MmPickmB.Column[]
            { MmPickmB.Column.cpickmid ,MmPickmB.Column.vbdef1,MmPickmB.Column.vbdef10,
                    MmPickmB.Column.cbmaterialid};

    private final MmPickmB.Column[] result2 = new MmPickmB.Column[]
            { MmPickmB.Column.cpickmid ,MmPickmB.Column.vbdef1,MmPickmB.Column.vbdef10,
                    MmPickmB.Column.cbmaterialid,MmPickmB.Column.nplanoutastnum,
                    MmPickmB.Column.cpickmBid,MmPickmB.Column.vbdef1,MmPickmB.Column.vbdef2,
                    MmPickmB.Column.vbdef3,MmPickmB.Column.nshouldnum,MmPickmB.Column.naccoutnum};



    @Resource
    private MmPickmMapper mmPickmMapper;
    @Resource
    private MmPickmBMapper mmPickmBMapper;

    /**
     * 根据id进行添加
     * @param cpickmid
     * @return
     */
    @Override
    public MmPickm findParentVoById(String cpickmid) {
        return mmPickmMapper.selectByPrimaryKey(cpickmid);
    }

    @Override
    public List<MmPickmB> findChildrenVoById(String cpickmid) {
        MmPickmBExample example = new MmPickmBExample();
        MmPickmBExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(cpickmid)) {
            criteria.andCpickmidEqualTo(cpickmid);
        }
        return mmPickmBMapper.selectByExample(example);
    }

    /**
     * 根据id进行删除
     * @param
     * @return
     */
    @Override
    public int deleteParentVoById(String cpickmid) {
        return mmPickmMapper.deleteByPrimaryKey(cpickmid);
    }
    @Override
    public int deleteChildrenVoById(String cpickmid) {
        return mmPickmBMapper.deleteByPrimaryKey(cpickmid);
    }

    /**
     * 根据备料计划主键进行查询
     * @param cfirstbillrowid
     * @return
     */
    @Override
    public MmPickm findParentVoByCfirstbillrowid(String cfirstbillrowid) {
        MmPickmExample example = new MmPickmExample();
        MmPickmExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(cfirstbillrowid)) {
            criteria.andCfirstbillrowidEqualTo(cfirstbillrowid);
        }
        MmPickm mmPickm = mmPickmMapper.selectOneByExampleSelective(example,result);
        return mmPickm;
    }

    /**
     * 根据备料计划来源(生产订单行)进行查询
     * @param vsourcemorowid
     * @return
     */
    @Override
    public MmPickm findParentVoByVsourcemorowid(String vsourcemorowid) {
        MmPickmExample example = new MmPickmExample();
        MmPickmExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(vsourcemorowid)) {
            criteria.andVsourcemorowidEqualTo(vsourcemorowid);
        }
        MmPickm mmPickm = mmPickmMapper.selectOneByExampleSelective(example,result);
        return mmPickm;
    }

    @Override
    public List<MmPickmB> findChildrenVoByCpickmid(String cpickmid) {
        MmPickmBExample example = new MmPickmBExample();
        example.or().andCpickmidEqualTo(cpickmid);
        return mmPickmBMapper.selectByExampleSelective(example,result1);
    }

    /**
     * 添加备料计划
     * @param mmPickm
     * @return
     */
    @Override
    public int addParentVo(MmPickm mmPickm){
        return mmPickmMapper.insert(mmPickm);
    }

    @Override
    public int addChildrenVo(MmPickmB mmPickmB) {

        return mmPickmBMapper.insert(mmPickmB);
    }

    /**
     * 修改备料计划
     * @param mmPickm
     * @return
     */
    @Override
    public int updateMmPickm(MmPickm mmPickm) {
        return mmPickmMapper.updateByPrimaryKey(mmPickm);
    }
    @Override
    public int updateMmPickmB(MmPickmB mmPickmB) {
        return mmPickmBMapper.updateByPrimaryKey(mmPickmB);
    }






    @Override
    public List<MmPickmB> findMmPickmBByCpickmid(String cpickmid) {
        MmPickmBExample example = new MmPickmBExample();
        example.or().andCpickmidEqualTo(cpickmid);
        return mmPickmBMapper.selectByExampleSelective(example,result2);
    }

    /**
     * 根据id查询MmPickmB
     * @param cpickmbid
     * @return
     */
    @Override
    public MmPickmB findMmPickmBById(String cpickmbid) {
        return mmPickmBMapper.selectByPrimaryKey(cpickmbid);
    }

    /**
     * 查询所有待收料记录
     * @return
     */
    @Override
    public List<MmPickm> findMmPickmRecords() {
        MmPickmExample example = new MmPickmExample();
        return mmPickmMapper.selectByExampleSelective(example);
    }

}
